#!/usr/bin/python
# -*- coding: UTF-8 -*-

"""
@author: andi
@contact: 643562684@qq.com
@time: 2022/5/16 17:39
"""
import json
import requests
import allure
from common.log import logger
from Conf.setting import PUBLIC_HEADERS


class RequestHandle(object):
    def __init__(self):
        self.req = requests.session()

    def get(self, url=None, **kwargs):
        """
        GET  请求
        :param data: 数据
        :param remarks: 备注
        :return:
        """

        kwargs = kwargs['kwargs'].get("kwargs")
        data = kwargs.get('data')
        _header = kwargs.get("headers")
        case_name = kwargs.get("case_name")
        headers = PUBLIC_HEADERS.update(_header) if _header else PUBLIC_HEADERS

        logger.info(f"请求的标题是: {case_name}")
        logger.debug(f"请求的地址是： {url}")
        logger.debug("请求的类型是： GET")
        logger.debug(f"请求数据: {data}")
        logger.debug(f"请求的头部信息: {headers}")

        allure.dynamic.title(case_name)
        allure.attach(f"请求的标题是: {case_name}", name='请求标题', attachment_type=allure.attachment_type.TEXT)
        allure.attach(f"请求的地址是： {url}", name='请求地址', attachment_type=allure.attachment_type.TEXT)
        allure.attach("请求的类型是： GET", name='请求类型', attachment_type=allure.attachment_type.TEXT)
        allure.attach(f"请求数据: {data}", name='请求数据', attachment_type=allure.attachment_type.TEXT)
        allure.attach(f"请求的头部信息: {headers}", name='请求的头部信息', attachment_type=allure.attachment_type.TEXT)

        try:
            resp = self.req.get(url=url, params=data, headers=headers)
        except Exception as e:
            logger.exception(f"{case_name}, 执行失败!!")
            raise e
        else:
            logger.info(f"请求成功: {json.dumps(resp.json(), sort_keys=True, indent=4, separators=(', ', ': '))}")
            return resp

    def post(self, url=None,  **kwargs):
        """
        POST  请求
        :param url:
        :param remarks:
        :param kwargs:
        :return:
        """
        kwargs = kwargs['kwargs'].get("kwargs")
        data = kwargs.get("data")
        json = kwargs.get("json")
        _header = kwargs.get("headers")
        case_name = kwargs.get("case_name")
        headers = PUBLIC_HEADERS.update(_header) if _header else PUBLIC_HEADERS

        logger.info(f"请求的标题是: {case_name}")
        logger.debug(f"请求的地址是： {url}")
        logger.debug("请求的类型是： POST")
        logger.debug(f"请求数据: {data or json}")
        logger.debug(f"请求的头部信息: {headers}")

        allure.dynamic.title(case_name)
        allure.attach(f"请求的标题是: {case_name}", name='请求标题', attachment_type=allure.attachment_type.TEXT)
        allure.attach(f"请求的地址是： {url}", name='请求地址', attachment_type=allure.attachment_type.TEXT)
        allure.attach("请求的类型是： POST", name='请求类型', attachment_type=allure.attachment_type.TEXT)
        allure.attach(f"请求数据: {data or json}", name='请求数据', attachment_type=allure.attachment_type.TEXT)
        allure.attach(f"请求的头部信息: {headers}", name='请求的头部信息', attachment_type=allure.attachment_type.TEXT)

        try:
            resp = self.req.post(url=url, data=data, json=json, headers=headers)
        except Exception as e:
            logger.exception(f"{case_name}, 执行失败!!")
            raise e
        else:
            logger.info(f"请求成功: {json.dumps(resp.json(), sort_keys=True, indent=4, separators=(', ', ': '))}")
            return resp
